library(ggplot2)
library(ggridges)
library(ggrepel)
library(viridis)
library(patchwork)

font_family <- ""
theme_set(
  theme_bw() +
    theme(
      legend.position = "bottom"
      # Roman font: see https://medium.com/@fulowa/latex-font-in-a-ggplot-9120caaa5250
      , text = element_text(size = 9, family = font_family)
      , axis.text = element_text(size = 7, family = font_family)
      , axis.title = element_text(size = 9, family = font_family)
      , strip.text = element_text(size = 9, family = font_family)
      , panel.grid.minor = element_blank()
      , panel.grid.major = element_line(color = "lightgrey", linewidth = 0.1)
      # , strip.background = element_rect(fill = NA)
    )
)

save_plot <- function(
  plot
  , filename  
  , path
  , devices = c("png")
  , ...
  , .units = "in"
  , .dpi = 300
  , .bg = "transparent"
) {
  stopifnot(
    "`path` does not exist" = dir.exists(path)
  )
  
  invisible(lapply(devices, function(dev) {
    ggsave(
      plot = plot
      , path = path
      , filename = paste0(filename, ".", dev)
      , device = dev
      , ...
      , dpi = .dpi
      , units = .units
      , bg = .bg
    )
  }))
}
